package com.zjeav.lingjiao.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.ItemResource;
import com.zjeav.lingjiao.base.baseBean.LocalMetadata;
import com.zjeav.lingjiao.base.tools.LocalUtils;
import com.zjeav.lingjiao.base.tools.TimeTools;
import com.zjeav.lingjiao.base.utils.UIUtils;

import org.xutils.DbManager;

import java.util.ArrayList;

public class MyExpandableListView extends BaseExpandableListAdapter {

    private Context context;
    //一级列表数据源
    private String[] groups = {"软件设计", "数据库技术", "操作系统"};
    //二级列表数据源
    private String[][] childs={{"架构设计","面向对象","设计模式","领域驱动设计"},{"SQL Server","Oracle","MySql", "Dameng "},{"Linux","Windows","嵌入式"}};

    private boolean isPayed;
    private int bookid;
    ArrayList<LocalMetadata> metadataArrayList;
    DbManager db;
    ClickListener listener;
    int type = 0;//要下载 1：播放 2：锁住

    public void isPayed(boolean isPayed) {
        this.isPayed = isPayed;
    }

    public MyExpandableListView(Context context, String[] groups,
                                String[][] childs,int bookid,DbManager db,ClickListener listener) {
        this.groups = groups;
        this.childs = childs;
        this.context = context;
        this.db = db;
        this.bookid = bookid;
        this.listener = listener;
    }

    /*一级列表个数*/
    @Override
    public int getGroupCount() {
        return groups.length;
    }

    /*每个二级列表的个数*/
    @Override
    public int getChildrenCount(int groupPosition) {
        return childs[groupPosition].length;
    }

    /*一级列表中单个item*/
    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    /*二级列表中单个item*/
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /*每个item的id是否固定，一般为true*/
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /*#TODO 填充一级列表
     * isExpanded 是否已经展开
     * */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_expandablelistview,null);
        }
        TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
        ImageView iv_group = (ImageView) convertView.findViewById(R.id.iv_group);
        tv_group.setText(groups[groupPosition]);
        //控制是否展开图标
        if (isExpanded) {
            iv_group.setImageResource(R.mipmap.arrow_down);
        } else {
            iv_group.setImageResource(R.mipmap.arrow_right);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.mate_resource_item,null);
        }

        String strInfoDo = childs[groupPosition][childPosition];
        final ItemResource infoDo = JSON.parseObject(strInfoDo, ItemResource.class);

        ImageView image = convertView.findViewById(R.id.mate_type_img);
        ImageView mate_down_img = convertView.findViewById(R.id.mate_down_img);
        TextView tvmate_content_txt = convertView.findViewById(R.id.mate_content_txt);
        TextView tvmate_dur_txt = convertView.findViewById(R.id.mate_dur_txt);
        final TextView tvmate_down_txt = convertView.findViewById(R.id.mate_down_txt);
        RelativeLayout music_ly = convertView.findViewById(R.id.music_ly);
        RelativeLayout relfrm = convertView.findViewById(R.id.relfrm);


        tvmate_content_txt.setText(infoDo.getTitle());
        tvmate_dur_txt.setText(TimeTools.getDuration(infoDo.getMetadata().getDuration()));

        if ("音频".equals(infoDo.getType())) {
            image.setImageResource(R.mipmap.data_audio);
        } else {
            image.setImageResource(R.mipmap.data_video);
        }

        tvmate_content_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.ItemClick(infoDo, type, 0,tvmate_down_txt);
            }
        });

        metadataArrayList = LocalUtils.getList(db, bookid);
     //   final int position = helper.getAdapterPosition();
        int position = 0;

        boolean isHas = false;
        if (metadataArrayList != null) {
            for (int i = 0; i < metadataArrayList.size(); i++) {
                if (infoDo.getId() == metadataArrayList.get(i).getId()) {
                    isHas = true;
                }
            }
        }

        music_ly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UIUtils.showToast("setOnClickListener");
            }
        });

        if (isPayed) {
            if (!isHas) {
                type = 0;
                mate_down_img.setImageResource( R.mipmap.data_download);
                mate_down_img.setVisibility(View.VISIBLE);
                tvmate_down_txt.setVisibility(View.GONE);
                music_ly.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (UIUtils.isFastClick()){
                           // listener.ItemClick(item, 0, position,helper.getView(R.id.mate_down_txt));
                        }
                    }
                });
            } else {
                type = 1;
                tvmate_down_txt.setText( "已下载");
                mate_down_img.setVisibility(View.GONE);
                tvmate_down_txt.setVisibility(View.VISIBLE);
                music_ly.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (UIUtils.isFastClick()){
                          //  listener.ItemClick(item, 1, position,helper.getView(R.id.mate_down_txt));
                        }

                    }
                });

            }
        } else {

            if (position == 0 || position == 1) {
                if (!isHas) {
                    mate_down_img.setImageResource( R.mipmap.file_locked);
                    mate_down_img.setVisibility(View.VISIBLE);
                    tvmate_down_txt.setVisibility(View.GONE);
                    music_ly.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (UIUtils.isFastClick()){
                            //    listener.ItemClick(item, 0, position,helper.getView(R.id.mate_down_txt));
                            }

                        }
                    });
                } else {
                    tvmate_down_txt.setText("已下载");
                    mate_down_img.setVisibility(View.GONE);
                    tvmate_down_txt.setVisibility(View.VISIBLE);
                    music_ly.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (UIUtils.isFastClick()){
                               // listener.ItemClick(item, 1, position,helper.getView(R.id.mate_down_txt));
                            }

                        }
                    });

                }
            } else {
                mate_down_img.setImageResource( R.mipmap.file_locked);
//                helper.addOnClickListener(R.id.mate_down_img);
                mate_down_img.setVisibility(View.VISIBLE);
                tvmate_down_txt.setVisibility(View.GONE);
                music_ly.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (UIUtils.isFastClick()){
                          //  listener.ItemClick(item, 2, position,helper.getView(R.id.mate_down_txt));
                        }

                    }
                });
            }
        }

        return convertView;
    }

    /*二级列表中每个能否被选中，如果有点击事件一定要设为true*/
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public interface ClickListener {
        void ItemClick(ItemResource itemResource, int type, int position,View view);
    }

}